import { defineStore } from 'pinia'
import { reactive } from 'vue'
import type { OssForm } from '@/api/pwd/type'

// 定义 Store
export const useOssStore = defineStore(
  'oss',
  () => {
    const ossForm = reactive<OssForm>({
      region: '',
      bucket: '',
      keyId: '',
      keySecret: '',
    })

    const setValue = (val: OssForm) => {
      console.log('oss setValue :', val)
      Object.assign(ossForm, val)
    }

    const clear = () => {
      Object.assign(ossForm, {})
    }

    const getToken = () => {
      console.log('getToken')
      console.log('region',ossForm.region)
      return `${ossForm.region}+${ossForm.keyId}+${ossForm.keySecret}+${ossForm.bucket}`
    }


    // 记得 return
    return {
      ossForm,
      setValue,
      clear,
      getToken
    }
  },
  {
    // 配置持久化
    persist: {
      // 调整为兼容多端的API
      storage: {
        setItem(key, value) {
          uni.setStorageSync(key, value) // [!code warning]
        },
        getItem(key) {
          return uni.getStorageSync(key) // [!code warning]
        },
      },
    },
  },
)
